package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"

	"github.com/antlinker/flow"
	"github.com/antlinker/flow/service/db"

	"github.com/teambition/gear"

	_ "github.com/go-sql-driver/mysql"
)

var (
	staticRoot string
	dsn        string
	addr       string
)

func init() {
	flag.StringVar(&staticRoot, "static", "", "静态目录")
	flag.StringVar(&dsn, "dsn", "myfoodiemall:Mall123456!@tcp(192.168.180.130:3306)/flow_test?charset=utf8", "MySQL链接路径")
	flag.StringVar(&addr, "addr", ":6062", "监听地址")
}

func main() {
	flag.Parse()

	flow.Init(
		db.SetDSN(dsn),
		db.SetTrace(true),
	)
	// err := flow.LoadFile("apply_sqltest.bpmn")
	// if err != nil {
	// 	// 处理错误
	// }

	serverOptions := []flow.ServerOption{
		flow.ServerStaticRootOption("./web"),
		flow.ServerPrefixOption("/flow/"),
		flow.ServerMiddlewareOption(filter),
	}

	http.Handle("/flow/", flow.StartServer(serverOptions...))

	log.Printf("服务运行在[%s]端口...", addr)
	log.Fatalf("%v", http.ListenAndServe(addr, nil))

	// err := flow.LoadFile("leave.bpmn")
	// if err != nil {
	// 	// 处理错误
	// }

	// input := map[string]interface{}{
	// 	"day": 3,
	// 	"bzr": "T001",
	// }
	// result, err := flow.StartFlow("process_leave_test", "node_start", "A001", input)
	// if err != nil {
	// 	// 处理错误
	// }

	// todos, err := flow.QueryTodoFlows("process_leave_test", "T001")
	// if err != nil {
	// 	// 处理错误
	// }

	// input := map[string]interface{}{
	// 	"fdy":    "T002",
	// 	"action": "pass",
	// 	"day":    1,
	// }

	// result, err := flow.HandleFlow("99cc8e2a-8059-43ae-a913-7fda623d7235", "T001", input)
	// if err != nil {
	// 	// 处理错误
	// 	fmt.Println(err)
	// }

	// fmt.Println("====", result)
}

func filter(ctx *gear.Context) error {
	fmt.Printf("请求参数：%s - %s \n", ctx.Path, ctx.Method)
	return nil
}
